//
//  DBReadInfoLogc.m
//  KidReading
//
//  Created by telen on 15/12/16.
//  Copyright © 2015年 刘赞黄Telen. All rights reserved.
//

#import "DBReadInfoLogc.h"
#import "ConstDefine.h"

NSString* const RKey_bookDel = @"bookDel";
NSString* const RKey_bookReadMode = @"bookReadMode";
NSString* const RKey_pageOperation = @"pageOperation";
NSString* const RKey_pageWord = @"pageWord";
NSString* const RKey_talkOperation = @"talkOperation";
NSString* const RKey_app = @"app";
NSString* const RKey_bookProgress = @"bookProgress";
NSString* const Rkey_Quiz = @"quiz";

NSString* const RMode_forme = @"forme";
NSString* const RMode_ido = @"ido";
NSString* const RMode_over = @"over";
NSString* const RMode_photo = @"photo";
NSString* const RMode_diyAudio = @"diyaudio";

NSString* const RMode_on = @"on";
NSString* const RMode_off = @"off";

NSString* const RMode_normal = @"normal";
NSString* const RMode_fast = @"fast";
NSString* const RMode_slow = @"slow";

NSString* const RType_enter = @"enter";
NSString* const RType_show = @"show";
NSString* const RType_graph = @"graph";
NSString* const RType_audio = @"audio";
NSString* const RType_trans = @"trans";
NSString* const RType_quit = @"quit";
NSString* const RType_followRead = @"followread";
NSString* const RType_hanzt = @"hztshow";

NSString* const RType_speed = @"speed";
NSString* const RType_pinyin = @"pinyin";

NSString* const RType_video = @"video";
NSString* const RType_share = @"share";

@implementation DBHelper(DBReadInfoLogc)

- (void)addReadInfo_bookdel:(NSInteger)bookId
{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
    //
    //{"bookId":1,"date":1234567890123,"userId":1}
    NSDictionary* dict = @{@"bookId":@(bookId),@"date":@(time),@"userId":@(uinfo.uid)};
    [self addReadingRecored:RKey_bookDel dict:dict];
}

- (void)addReadInfo_bookReadMode:(NSInteger)bookId mode:(NSString *)mode
{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
    //
    //{"bookId":1,"mode":"forme/ido","date":1234567890123,"userId":1}
    NSDictionary* dict = @{@"bookId":@(bookId),@"mode":mode,@"date":@(time),@"userId":@(uinfo.uid)};
    [self addReadingRecored:RKey_bookReadMode dict:dict];
}

- (void)addReadInfo_pageOperation:(NSInteger)bookId pageIndex:(NSInteger)pageindex type:(NSString *)type mode:(NSString *)mode
{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
    //
    //{"bookId":1,"pageIndex":1,"type":"speed","mode":"normal/fast/slow","date":1234567890123,"userId":1}
    NSDictionary* dict = @{@"bookId":@(bookId),@"pageIndex":@(pageindex),@"type":type,@"mode":mode,@"date":@(time),@"userId":@(uinfo.uid)};
    [self addReadingRecored:RKey_pageOperation dict:dict];
}

- (void)addReadInfo_pageWord:(NSInteger)bookId pageIndex:(NSInteger)pageindex type:(NSString *)type word:(NSString *)word
{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
    //
    //{"bookId":1,"pageIndex":1,"type":"show/graph/audio/","word":"你好","date":1234567890123,"userId":1}
    NSDictionary* dict = @{@"bookId":@(bookId),@"pageIndex":@(pageindex),@"type":type,@"word":word,@"date":@(time),@"userId":@(uinfo.uid)};
    [self addReadingRecored:RKey_pageWord dict:dict];
}
//流式阅读器阅读进度统计
- (void)addFlowReadProgressInfoWithBookId:(NSInteger)bookId ReadProgress:(CGFloat)readProgress
{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
    //
    NSDictionary* dict = @{@"bookId":@(bookId),@"progress":@(readProgress),@"userId":@(uinfo.uid),@"date":@(time)};
    [self addReadingRecored:RKey_bookProgress dict:dict];
}

- (void)addReadInfo_talkOperation:(NSInteger)bookId type:(NSString *)type
{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
    //
    //{"bookId":1,"type":"video/share","date":123457890123,"userId":1}
    NSDictionary* dict = @{@"bookId":@(bookId),@"type":type,@"date":@(time),@"userId":@(uinfo.uid)};
    [self addReadingRecored:RKey_talkOperation dict:dict];
}

- (void)addReadInfo_app_type:(NSString *)type
{
    UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
    LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
    NSString* osVer = [UIDevice currentDevice].systemName;
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString* appVer = [infoDic objectForKey:@"CFBundleShortVersionString"];
    //
    //{"userId":1,"osVer":"9.1.0","platform":"ios","appVer":"3.0.0","type":"enter/quit","date":1234567890123}
    NSDictionary* dict = @{@"osVer":osVer,@"appVer":appVer,@"platform":@"ios",@"type":type,@"date":@(time),@"userId":@(uinfo.uid)};
    [self addReadingRecored:RKey_app dict:dict];
}

//quiz数据统计
- (void)addQuizInfoWithQuizDict:(NSDictionary *)resultDict {
    if (resultDict) {
        UserInfo* uinfo = [CacheData shareSingle].getUserLoginInfo;
        LongLong time = [DataProcessing timestampFromNSDate:[NSDate date]];
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString* appVer = [infoDic objectForKey:@"CFBundleShortVersionString"];
        NSDictionary* dict = @{@"appVer":appVer,@"platform":@"ios",@"date":@(time),@"userId":@(uinfo.uid)};
        NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:dict];
        [mDict setValuesForKeysWithDictionary:resultDict];
        [self addReadingRecored:Rkey_Quiz dict:mDict];
    }
}


//
- (void)readInfo_Dicts_sendToNet
{
    [DataProcessing do_Async_quick:^{
        [self sendNetLocalReadRecord_Dict_complete:nil];
    }];
}

@end
